<template>
  <el-dialog :visible.sync="isShow" title="新增部门" width="500">
    <el-form label-width="100px" :model="form" :rules="rules" ref="form">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select @focus="focusEvent" v-model="form.manager">
          <el-option
            v-for="i in selectList"
            :key="i.id"
            :label="i.username"
            :value="i.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align:center">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysUserSimple } from '@/api/departments.js'
export default {
  name: 'Dialog',
  props: {
    initDate: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      item: '', // 当前点击项数据
      isShow: false,
      selectList: [],
      form: {
        name: '', // ,string,非必须,部门名称
        code: '', // ,string,非必须,部门编码，同级部门不可重复
        manager: '', // ,string,非必须,负责人名称
        introduce: '', // ,string,非必须,介绍
        pid: '' // ,string,非必须,父级部门ID
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callbcak) => {
              // 筛选出同级部门
              const temp = this.initDate.filter(item => {
                // 返回ture就是要满足这个条件的
                return item.pid === this.item.id
              })
              // 判断部门名称是否重名
              const result = temp.some(item => {
                return item.name === value.trim()
              })
              result
                ? callbcak(new Error(`部门名字（${value}）重复,请修改！`))
                : callbcak()
            }
          }
        ], // ,string,非必须,部门名称
        code: [{ required: true, message: '必填', trigger: 'change' }], // ,string,非必须,部门编码，同级部门不可重复
        manager: [{ required: true, message: '必填', trigger: 'change' }], // ,string,非必须,负责人名称
        introduce: [{ required: true, message: '必填', trigger: 'change' }] // ,string,非必须,介绍
      }
    }
  },
  mounted () {
    this.$bus.$on('show', (bol, item) => {
      console.log('测试', item)
      this.isShow = bol
      // 当前点击项的数据
      this.item = item
    })
  },
  beforeDestroy () {
    this.$bus.$off('show')
  },
  methods: {
    async focusEvent () {
      console.log(this.initDate)
      // 没有数据才获取
      if (this.selectList.length === 0) {
        const res = await sysUserSimple()
        console.log('员工基本信息', res)
        this.selectList = res.data
      }
    },
    // 全局表单验证
    submit () {
      this.$refs.form.validate(result => {
        if (result) {
          this.$message.success('验证成功')
        } else {
          this.$message.error('验证失败')
        }
      })
    }
  }
}
</script>

<style></style>
